package com.cgbl.service.vm

import android.app.Application
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.BaseResponse
import com.robot.base.model.MemberAdapterBean
import com.robot.base.model.PageListBean
import com.robot.base.model.ProjectBean
import com.robot.base.model.TeamListBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent

class ProjectListViewModel(application: Application) : GLBaseViewModel(application) {

    val keywordField = ObservableField<String>()
    val tabType = ObservableInt(TAB_ALL)

    var pageNo = 1

    val pageSize = 10

    val clickAllTypeCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_ALL)
        refresh()
    })

    val clickPendingStartCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_PENDING_START)
        refresh()
    })

    val clickPaymentCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_PAYMENT)
        refresh()
    })

    val clickDevCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_DEV)
        refresh()
    })

    val clickCompleteCommand = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_COMPLETE)
        refresh()
    })

    fun refresh() {
        pageNo = 1
        fetchProject()
    }

    fun loadMore() {
        pageNo++
        fetchProject()
    }

    private fun fetchProject() {
        val status = tabType.get()
        val keyword = keywordField.get() ?: ""
        val params = if (status == 0) {
            mapOf("pageNo" to pageNo, "pageSize" to pageSize, "itemName" to keyword)
        } else {
            mapOf(
                "pageNo" to pageNo,
                "pageSize" to pageSize,
                "status" to status,
                "itemName" to keyword
            )
        }
        WorkServiceFactory.getCustomerItemInfo(params).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ProjectBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ProjectBean>>) {
                if (pageNo == 1) {
                    uc.refreshProjectDataEvent.postValue(t.data.list)
                } else {
                    uc.loadMoreProjectDataEvent.postValue(t.data.list)
                }
            }
        })
    }


    val uc = UIChangeEvent()

    class UIChangeEvent {

        val refreshProjectDataEvent = SingleLiveEvent<List<ProjectBean>>()

        val loadMoreProjectDataEvent = SingleLiveEvent<List<ProjectBean>>()

    }


    companion object {

        const val TAB_ALL = 0

        const val TAB_PENDING_START = 1

        const val TAB_PAYMENT = 2

        const val TAB_DEV = 3

        const val TAB_COMPLETE = 4

    }

}